package edu.gyc.hishiro.controller;

import edu.gyc.hishiro.model.Myuser;
import edu.gyc.hishiro.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@Slf4j
public class LoginController {

    @RequestMapping("/dologin")
    public String login(UserInfo myuser, Model model) {
        log.debug("called login");
        Subject currentUserSubject = SecurityUtils.getSubject();
        if (!currentUserSubject.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(myuser.getUsername(), myuser.getPassword());

            String msg = "";
            try {
                currentUserSubject.login(token);
            } catch (UnknownAccountException exception) {

                msg = "UnknownAccountException -- > 账号不存在：";
            } catch (IncorrectCredentialsException exception) {
                msg = "IncorrectCredentialsException -- > 密码不正确：";
            }
            model.addAttribute("msg", msg);
        }

        return "login";
    }

//    @RequestMapping("/logout")
//    public String logout() {
//
//        Subject currentUserSubject = SecurityUtils.getSubject();
//         currentUserSubject.logout();
//        System.out.println("用户退出");
//
//        return "/login";
//    }

}
